package com.explore.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.explore.demo.VO.CommentInfoVO;
import com.explore.demo.common.ResultCode;
import com.explore.demo.common.ServerResponse;
import com.explore.demo.config.JwtConfig;
import com.explore.demo.mapper.UserMapper;
import com.explore.demo.pojo.Comment;
import com.explore.demo.mapper.CommentMapper;
import com.explore.demo.pojo.User;
import com.explore.demo.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaolainan
 * @since 2021-04-14
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService
{

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private JwtConfig jwtConfig;

    @Override
    public ServerResponse addComment(Comment comment, HttpServletRequest request)
    {
        if (comment == null){
            return ServerResponse.createBySuccessMessage(ResultCode.COMMENT_IS_NULL.getDescription(),
                                                         ResultCode.COMMENT_IS_NULL.getCode(),
                                                         null);
        }
        String token = request.getHeader(jwtConfig.getHeader());
        String account = jwtConfig.getUsernameFromToken(token);
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("account", account));
        comment.setUserId(user.getId());
        return commentMapper.insert(comment) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.COMMENT_INSERT_FAIL.getDescription(),
                                                     ResultCode.COMMENT_INSERT_FAIL.getCode(),
                                                     null);
    }

    @Override
    public ServerResponse deleteCategoryByRecruitId(String recruitId)
    {
        if (recruitId == null){
            return ServerResponse.createBySuccessMessage(ResultCode.COMMENT_ID_IS_NULL.getDescription(),
                                                         ResultCode.COMMENT_ID_IS_NULL.getCode(),
                                                         null);
        }
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper
                .eq("recruit_id",recruitId);
        Comment comment = commentMapper.selectOne(wrapper);
        UpdateWrapper<Comment> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .set("deleted",1);
        return commentMapper.update(comment,updateWrapper) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.COMMENT_DELETE_FAIL.getDescription(),
                                                     ResultCode.COMMENT_DELETE_FAIL.getCode(),
                                                     null);
    }

    @Override
    public ServerResponse queryAll()
    {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper
                .ge("id",1);
        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     commentMapper.selectList(wrapper));
    }

    @Override
    public ServerResponse queryCommentByRecruitId(String recruitId)
    {
        if (recruitId == null){
            return ServerResponse.createBySuccessMessage(ResultCode.COMMENT_ID_IS_NULL.getDescription(),
                                                         ResultCode.COMMENT_ID_IS_NULL.getCode(),
                                                         null);
        }
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper
                .eq("recruit_id",recruitId)
                .orderByDesc("create_time");
        List<Comment> comments = commentMapper.selectList(wrapper);
        List<CommentInfoVO> res = new ArrayList<>();
        for (Comment tmp : comments){
            CommentInfoVO commentInfoVO = new CommentInfoVO();
            User user = userMapper.selectOne(new QueryWrapper<User>().eq("id", tmp.getUserId()));
            commentInfoVO.setComment(tmp)
                         .setNickName(user.getNickName())
                         .setHeadPortrait(user.getHeadPortrait());
            res.add(commentInfoVO);
        }

        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     res);
    }

    @Override
    public ServerResponse updateComment(Comment comment)
    {
        if (comment == null){
            return ServerResponse.createBySuccessMessage(ResultCode.COMMENT_IS_NULL.getDescription(),
                                                         ResultCode.COMMENT_IS_NULL.getCode(),
                                                         null);
        }
        return commentMapper.updateById(comment) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.UPDATE_FAIL.getDescription(),
                                                     ResultCode.UPDATE_FAIL.getCode(),
                                                     null);
    }
}
